#ifndef TINY_MUDUO_TCPSERVER_H_
#define TINY_MUDUO_TCPSERVER_H_

#include <memory>
#include <map>

#include "callback.h"
#include "eventloop.h"
#include "acceptor.h"
#include "eventloopthreadpool.h"
#include "tcpconnection.h"
#include "noncopyable.h"
//能用move操作减少字符串拷贝操作的尽量都用move操作
namespace tiny_muduo{
    class Address;

    class TcpServer:public NonCopyAble{
    public:
        TcpServer(EventLoop* loop, const Address &address);
        ~TcpServer();

        void Start()
        {
            threads_->StartLoop();
            loop_->RunOneFunc(std::bind(&Acceptor::Listen,acceptor_.get()));
        }

        void SetConnectionCallback(ConnectionCallback&& callback){
            connection_callback_=std::move(callback);
        }

        void SetMessageCallback(MessageCallback&& callback){
            message_callback_=std::move(callback);
        }

        void SetThreadNums(int thread_nums){
            threads_->SetThreadNums(thread_nums);
        }

        void HandleClose(const TcpConnectionPtr& conn);
        void HandleCloseInLoop(const TcpConnectionPtr& ptr);
        void HandleNewConnection(int connfd);
    private:
        typedef std::map<int,TcpConnectionPtr> ConnectionMap;

        EventLoop *loop_;
        std::unique_ptr<EventLoopThreadPool> threads_;
        std::unique_ptr<Acceptor> acceptor_;

        ConnectionCallback connection_callback_;
        MessageCallback message_callback_;
        ConnectionMap connections_;
    };
}
#endif